package iris_controller

import (
	"github.com/kataras/iris/v12"
	"gluenet/modules/manager/server/domain"
	"gluenet/modules/manager/server/infra"
	"gluenet/modules/manager/server/infra/prepare"
)

func init() {
	prepare.RegisterAuthController(func() {
		controller := NewAgentController()
		prepare.InjectAuthController(func() *Agent {
			return controller
		})
	})
}

type Agent struct {
	Agent *domain.Agent
}

func NewAgentController() *Agent {
	r := &Agent{}
	prepare.FetchDependency(&r.Agent)
	return r
}

// Get serves
// Method:   GET
// Resource: http://localhost:8080/apis/agent/all/runtime
func (c *Agent) GetAllRuntime(ctx iris.Context) *infra.Response {
	objs, err := c.Agent.FindRuntime()
	if err != nil {
		return infra.ResponseReqErr(err, nil)
	}
	return infra.ResponseOK(objs)
}

// Get serves
// Method:   GET
// Resource: http://localhost:8080/apis/agent/all/runtime/list
func (c *Agent) GetAllRuntimeList(ctx iris.Context) *infra.Response {
	objs, err := c.Agent.FindRuntimeList()
	if err != nil {
		return infra.ResponseReqErr(err, nil)
	}
	return infra.ResponseOK(objs)
}

// Get serves
// Method:   GET
// Resource: http://localhost:8080/apis/agent/all
func (c *Agent) GetAll(ctx iris.Context) *infra.Response {
	objs, err := c.Agent.FindAll()
	if err != nil {
		return infra.ResponseReqErr(err, nil)
	}
	return infra.ResponseOK(objs)
}
